# -*- encoding: utf-8 -*-
##############################################################################
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see http://www.gnu.org/licenses/.
#
##############################################################################

from openerp import models, fields, api
import re

class mrp_bom(models.Model):
    _inherit = 'mrp.bom'

    use_reinforcement_drawing = fields.Boolean(u'是否使用配筋', required=False,help=u'系统将自动选择合适的配筋方案计算所需钢筋用量', )

    @api.model
    def _bom_explode(self, bom, product, factor, properties=None, level=0,
                     routing_id=False, previous_products=None,
                     master_bom=None):
        result, result2 = super(mrp_bom, self)._bom_explode(
            bom, product, factor, properties=properties, level=level,
            routing_id=routing_id, previous_products=previous_products,
            master_bom=master_bom)
        if bom.use_reinforcement_drawing:
            lwh = re.findall(r'\d+',product.attribute_1.name)
            l = int(lwh[0])
            w = int(lwh[1])
            h = int(lwh[2])
            domain = [('product_type_id','=',product.attribute_2.id),('from_length','<=',l),('to_length','>=',l),('heigth','=',h)]
            scheme = self.env['reinforcement.scheme'].search(domain,limit=1)
            print(scheme)
            layout = scheme.compute_products(l,w)
            gj = {}
            # print(layout)
            for a in layout:
                for b in a:
                    for c in b:
                        h = c['horizontal']
                        v = c['vertical']
                        def add_qty(des):
                            diameter =des['diameter']
                            length =des['length']
                            qty = des['qty']
                            print(gj,diameter,length,qty)
                            if gj.has_key(diameter):
                                if gj[diameter].has_key(length):
                                    gj[diameter][length] = gj[diameter][length] + qty
                                else:
                                    gj[diameter][length] = qty
                            else:
                                gj[diameter] = {length:qty}
                        add_qty(h)
                        add_qty(v)
            for diameter in gj:
                sum_length = sum(l*gj[diameter][l] for l in gj[diameter])
                product_id = self.env['product.product'].search([('name','ilike',u'钢筋'),('attribute_1','=',diameter.id)],limit=1)
                print(diameter,sum_length,product_id)
                result.append({
                        'name': product_id.name,
                        'product_id': product_id.id,
                        'product_qty': sum_length/1000,
                        'product_uom': 8,   #米
                        'product_uos_qty': False,
                        'product_uos': False,
                    })
        print(result)
        return result, result2

